package com.opgea.crm.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.opgea.crm.common.vo.ContactVo;
import com.opgea.crm.common.vo.LeadVo;
import com.opgea.crm.domain.modal.JsonModelMap;
import com.opgea.crm.services.AdditionalContactService;
import com.opgea.crm.services.CustomerService;

@Controller
@RequestMapping(value="searchcustomers")
public class CustomerSearchController {

	Logger logger=Logger.getLogger(LeadSearchController.class);
	@Autowired
	private CustomerService customerService;
	@Autowired
	private AdditionalContactService contactService;
	
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView show(HttpServletRequest request){
		return new ModelAndView("searchcustomers");
	}

	@RequestMapping(value="showCustomers",method=RequestMethod.GET)
	public @ResponseBody
	Map<String,Object> showLeads(@RequestParam(required=false) String criteria,
								 @RequestParam(required=false) String value){
		if(criteria != null && value != null){
			List<LeadVo> list = customerService.findDynamically(criteria, value);
			return JsonModelMap.success().data(list);
		}else{
			return JsonModelMap.success();
		}
	}
	
	@RequestMapping(value="showContacts",method=RequestMethod.GET)
	public @ResponseBody
	Map<String,Object> showContacts(@RequestParam(required=true) String customerId)
	{
		logger.debug("SearchLead >> showContacts");
		List<ContactVo> list = contactService.findAllByLeadId(new Long(customerId));
		return JsonModelMap.success().data(list);
	}	
}

